package com.rayboot.footpoint.db;

import com.rayboot.footpoint.R;
import com.rayboot.footpoint.activity.NewFootpointActivity;
import com.rayboot.footpoint.db.TableFootprints.FootprintColumns;
import com.rayboot.footpoint.model.Category;
import com.rayboot.footpoint.model.Footprint;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TableCategory extends IBaseTable {

	public TableCategory(SQLiteDatabase db) {
		super(db);
		// TODO Auto-generated constructor stub
		create();

		initDB();
	}

	private void initDB() {
		// TODO Auto-generated method stub
		Cursor cursor = getAllData();
		if (cursor.getCount() == 0) {
			insert(new Category(R.drawable.icon_favorites, "favorite",
					"Favorites"));
			insert(new Category(R.drawable.icon_shopping, "shopping",
					"Shopping"));
			insert(new Category(R.drawable.icon_restaurant, "dining", "Dining"));
			insert(new Category(R.drawable.icon_leisure, "leisure", "Leisure"));
			insert(new Category(R.drawable.icon_others, "other", "Others"));
		}
	}

	@Override
	public void create() {
		// TODO Auto-generated method stub/
		String CREATE_DOWNLOAD_TBL_SQL = "CREATE TABLE IF NOT EXISTS "
				+ CategoryColumns.CATEGORY_TBL + "(" + CategoryColumns.ICON_ID
				+ " TEXT," + CategoryColumns.KEYWORD + " TEXT,"
				+ CategoryColumns.NAME + " TEXT" + ")";
		db.execSQL(CREATE_DOWNLOAD_TBL_SQL);
	}

	@Override
	public boolean insert(Object obj) {
		// TODO Auto-generated method stub
		Category category = (Category) obj;
		ContentValues values = new ContentValues();
		values.put(CategoryColumns.ICON_ID, category.mIconID);
		values.put(CategoryColumns.KEYWORD, category.mKeyword);
		values.put(CategoryColumns.NAME, category.mName);

		return db.insert(CategoryColumns.CATEGORY_TBL, "null", values) >= 0;
	}

	@Override
	public boolean update(Object obj) {
		// TODO Auto-generated method stub
		Category category = (Category) obj;
		ContentValues values = new ContentValues();
		values.put(CategoryColumns.ICON_ID, category.mIconID);
		values.put(CategoryColumns.KEYWORD, category.mKeyword);
		values.put(CategoryColumns.NAME, category.mName);

		String[] params = new String[] { category.mName };
		return db.update(CategoryColumns.CATEGORY_TBL, values,
				CategoryColumns.NAME + "=?", params) >= 0;
	}

	@Override
	public int remove(Object obj) {
		// TODO Auto-generated method stub
		Category category = (Category) obj;
		String[] params = new String[] { category.mName };
		return db.delete(CategoryColumns.CATEGORY_TBL, CategoryColumns.NAME
				+ "=?", params);
	}

	@Override
	public Cursor getAllData() {
		// TODO Auto-generated method stub
		Cursor cursor = db.query(CategoryColumns.CATEGORY_TBL, null, null,
				null, null, null, null);
		return cursor;
	}

	@Override
	public void drop() {
		// TODO Auto-generated method stub
		db.execSQL("drop table if exists " + CategoryColumns.CATEGORY_TBL);
	}

	@Override
	public int removeAllData() {
		// TODO Auto-generated method stub
		return db.delete(CategoryColumns.CATEGORY_TBL, null, null);
	}

	public static final class CategoryColumns {
		public static final String CATEGORY_TBL = "category_tbl";
		public static final String ICON_ID = "icon_id";
		public static final String KEYWORD = "keyword";
		public static final String NAME = "name";
	}

	@Override
	public Cursor getDataBySomething(String rule) {
		// TODO Auto-generated method stub
		return null;
	}
}
